#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_sendButton_clicked();
    void on_connectButton_clicked();
    void handleNewConnection();
    void handleClientDisconnected();
    void handleReadyRead();
    void handleConnected();
    void handleDisconnected();
    void handleError(QAbstractSocket::SocketError socketError);

private:
    Ui::MainWindow *ui;
    QTcpServer *server;
    QTcpSocket *socket;
    QTcpSocket *clientConnection;

    void sendMessage(const QString &message);
    void appendMessage(const QString &message);
    bool eventFilter(QObject *obj, QEvent *event) override;  // 添加这一行
    bool isServerMode;
};

#endif // MAINWINDOW_H
